package filter

import (
	"bufio"
	"com.jiaguyu/until"
	"io"
	"os"
)

var (
	trie *until.Trie
)

func Init(fileName string) (err error) {
	//生成trie
	trie = until.NewTrie()
	//读取 敏感词文件
	file, err := os.Open(fileName)
	if err != nil {
		return
	}
	//打开文件 读取完整 关闭
	defer file.Close()
	reader := bufio.NewReader(file)
	for {
		word, errRet := reader.ReadString('\n')
		if errRet == io.EOF {
			err = nil
			return
		}
		if errRet != nil {
			err = errRet
			return
		}

		err = trie.Add(word, nil)
		if err != nil {
			return
		}
	}
	return
}
